# frozen_string_literal: true

class RuleSetRule < ApplicationRecord
  include Ordinalable

  belongs_to :rule_set, class_name: 'RuleEntity::RuleSet'
  belongs_to :rule_entity

  validates :rule_set_id, presence: true
  validates :rule_entity_id, presence: true
  validates :ordinal, presence: true
  validate :avoid_endless_loop

  private

  def avoid_endless_loop
    return unless rule_set_id&.eql?(rule_entity_id)

    errors.add(:rule_set_id, 'and rule_entity_id cannot be equal to avoid an endless loop')
  end
end
